﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dashboard.Models;

namespace Dashboard.Models
{
    internal class LogsManager : ILogsManager
    {
        private const string WindowsLogsDirectory = "C:/Windows/System32/winevt/Logs";

        private static LogsManager instance = new LogsManager();
        internal static ILogsManager Instance
        {
            get { return instance; }
        }

        private LogsManager()
        {

        }

        public List<Log> GetLogs()
        {
            EventLog[] eventLogs = EventLog.GetEventLogs();
            List<Log> list = new List<Log>();

            for (int i = 0; i < eventLogs.Length; i++)
            {
                list.Add(new Log(eventLogs[i].Log, eventLogs[i].Log));
            }

            return list;
        }

        public FilePathInfo GetPathLog(string log)
        {
            if (String.IsNullOrEmpty(log))
            {
                throw new ArgumentException("id is null or empty");
            }

            Process myProcess = new Process();

            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.FileName = "C:\\Users\\Leva\\Desktop\\New folder (2)\\AccessToSystem32\\AccessToSystem32\\bin\\Debug\\AccessToSystem32.exe";
            myProcess.Start();

            DirectoryInfo LogsDirectoryInfo = new DirectoryInfo(WindowsLogsDirectory);
            FileInfo[] files = LogsDirectoryInfo.GetFiles();
            bool existing = false;

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name == log)
                {
                    Console.WriteLine(files[i].Name);
                    existing = true;
                    break;
                }
            }

            if (!existing)
            {
                throw new ArgumentException("file was not found");
            }

            string pathToTempFile = String.Concat(Environment.GetEnvironmentVariable("temp"), "\\", log);
            string logPath = String.Concat(WindowsLogsDirectory, "\\", log);

            if (!System.IO.File.Exists(pathToTempFile))
            {
                System.IO.File.Copy(logPath, pathToTempFile);
            }
            else
            {
                System.IO.File.Delete(pathToTempFile);
                System.IO.File.Copy(logPath, pathToTempFile);
            }

            return new FilePathInfo("C:/", "application/octet-stream", log);
        }
    }
}